package com.lyn.plugin.proxy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.lyn.plugin.entity.Plugin;
import com.lyn.plugin.PluginManager;

public class ProxyActivity extends Activity {

    public PluginInterface pluginInterface;
    public Plugin plugin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            Intent intent = getIntent();
            String path = intent.getStringExtra("path");
            plugin = PluginManager.loadPlugin(getApplicationContext(),path);
            assert plugin != null;
            plugin.activity = this;
            int code = intent.getIntExtra("code", PluginManager.PLUGIN_OTHER_CODE);
            if (code == PluginManager.PLUGIN_LAUNCHER_CODE){
                Class<?> aClass = plugin.classLoader.loadClass(plugin.launcherClassName);
                pluginInterface = (PluginInterface) aClass.newInstance();
            }else {
                String className = intent.getStringExtra("className");
                Class<?> aClass = plugin.classLoader.loadClass(className);
                pluginInterface = (PluginInterface) aClass.newInstance();
            }
            pluginInterface.onPlugin(plugin);
            pluginInterface.onLifeCycle(getCurrentMethodName(),savedInstanceState);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        pluginInterface.onLifeCycle(getCurrentMethodName(), intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        pluginInterface.onLifeCycle(getCurrentMethodName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        pluginInterface.onLifeCycle(getCurrentMethodName());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        pluginInterface.onLifeCycle(getCurrentMethodName());
    }

    @Override
    protected void onStop() {
        super.onStop();
        pluginInterface.onLifeCycle(getCurrentMethodName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        pluginInterface.onLifeCycle(getCurrentMethodName());
    }

    @Override
    protected void onDestroy() {
        pluginInterface.onLifeCycle(getCurrentMethodName());
        super.onDestroy();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        pluginInterface.onLifeCycle(getCurrentMethodName(), requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pluginInterface.onLifeCycle(getCurrentMethodName(), requestCode, resultCode, data);
    }

 
    public String getCurrentMethodName() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        StackTraceElement element = stackTrace[3];
        return element.getMethodName();
    }
}
